/**
* Copyright (C) 2007 Pedro Miller Rabinovitch.
* http://www.opensource.org/licenses/mit-license.php
*
* Basically, feel free to use it however you want to.
* Please give me credit where appropriate.
*
* Pedro "Shade" Miller, gmail user: pedromr
* http://exodia.org
*
* @author Miller
*
* $Id: AgentShape.as 7 2007-12-13 10:07:21Z pedromr $
*/
package org.exodia.seeds.sprites {
	
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import org.exodia.math.Vector3D;
	import org.exodia.seeds.model.Agent;
	import org.exodia.seeds.sprites.WorldShape;

	public class AgentShape extends Sprite {
		private var _agent:Agent;
		public var _drawRadius:Boolean;
		public var _drawNeighbours:Boolean;
		public var rgbFillColor:uint;
		
		public function AgentShape( agent:Agent ) {
			super();
			this._agent = agent;
			_drawNeighbours = false;
			_drawRadius = false;
			rgbFillColor = 0xA4D1FF;
			draw();
			addEventListener( Event.RENDER, handleRender );
		}
		
		public function get agent() :Agent {
			return _agent;
		}

		private function drawNeighbours() : void {
			var g:Graphics = graphics;
			g.lineStyle( 1, 0xC0C0C0 );
			
			if( _drawRadius )
				g.drawCircle( 0, 0, Math.sqrt( agent.sensorRangeSquared ));
			
			if( _drawNeighbours ) {
				for each( var other:Agent in _agent.neighbours ) {
					g.moveTo(0,0);
					// como nao tem transformacao funciona, mas com zoom ia dar errado...
					// tinha que ser de shape para shape, mas a posicao pode nao ter sido
					// atualizada ainda (provavelmente nao foi!).
					g.lineTo( other.position.x - _agent.position.x, other.position.y - _agent.position.y );
				}
			}
		}
		
		private function draw() : void {
			var g:Graphics = graphics;
			g.clear();
			
			g.beginFill( rgbFillColor );
			g.lineStyle( 1, 0x000033 );
			g.drawCircle( 0, 0, agent.radius );
			g.endFill();

			if( _drawNeighbours || _drawRadius )
				drawNeighbours();
				
			if ( true ) {
				g.lineStyle( 1, 0x009100 );
				g.moveTo( 0, 0 );
				g.lineTo( _agent.speed.x * 400, _agent.speed.y * 400 );
			}
			if( true ) {
				g.lineStyle( 1, 0x910000 );
				g.moveTo( 0, 0 );
				g.lineTo( _agent.thrust.x * 250, _agent.thrust.y * 250 );
					
			}
		}
		
		private function handleRender( evt:Event ) {
			draw();
			
			x = _agent.position.x;
			y = _agent.position.y;
			
			//trace( "render at "+[x,y] );
		}
	}
	
}
